package com.example.im.model;

import android.content.Context;

import com.example.im.model.bean.UserInfo;
import com.example.im.model.dao.UserAccountDao;
import com.example.im.model.db.DBManger;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//数据模型层全局类
public class Model {
    //上下文对象
    private Context mContext;
    //获取线程池
    private ExecutorService executors = Executors.newCachedThreadPool();
    //创建对象
    private static Model model = new Model();
    //用户账号数据库的操作类
    UserAccountDao userAccountDao;
    //联系人和邀请信息表的操作类的管理类
    private DBManger dbManger;

    //私有化构造
    private Model() {
    }

    //获取单例对象
    //得到这个对象对本类的方法进行调用
    public static Model getInstance(){
        return model;
    }

    //初始化的方法
    public void init(Context context){
        mContext = context;

        //创建用户账号数据库的操作类对象
        userAccountDao = new UserAccountDao(mContext);

        //开启全局监听
        EventListener eventListener = new EventListener(mContext);
    }

    //获取全局线程池对象
    public ExecutorService getGlobalThreadPool(){
        return executors;
    }

    //用户登录成功后的处理方法
    public void loginSuccess(UserInfo account){
        //校验
        if (account == null){
            return;
        }
        if (dbManger != null){
            dbManger.close();
        }
        dbManger = new DBManger(mContext, account.getName());
    }

    //获取联系人和邀请信息表的操作类的管理类对象
    public DBManger getDbManger(){
        return dbManger;
    }


    //获取用户账号数据库的操作类对象
    public UserAccountDao getUerAccountDao(){
        return userAccountDao;
    }
}
